package crmdna.custom.isha;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.program.CheckIn;
import crmdna.client.program.Program;
import crmdna.client.program.Program.SessionProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;

@Api(name = "isha")
public class IshaProgramApi {
	
	@ApiMethod(path = "getOngoingSessions", httpMethod = HttpMethod.GET)
	public APIResponse getOngoingSessions(@Named("dateYYYYMMDD") int dateYYYYMMDD, User user) {
		try {
									
			List<SessionProp> sessionProps = Program.getOngoingSessions("isha", 
					dateYYYYMMDD, Utils.getLoginEmail(user)); 
					new ArrayList<>();			
			
			return new APIResponse(Status.SUCCESS, sessionProps);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(path = "checkin", httpMethod = HttpMethod.POST)
	public APIResponse checkin(@Named("programId") long programId, 
			@Named("memberId") long memberId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD, 
			@Named("batchNo") int batchNo, User user) {
		try {
			
			int numCheckins = CheckIn.checkin("isha", memberId, 
					programId, sessionDateYYYYMMDD, batchNo, 
					Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, numCheckins);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}	
	
	@ApiMethod(path = "checkout", httpMethod = HttpMethod.POST)
	public APIResponse checkout(@Named("programId") long programId, 
			@Named("memberId") long memberId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD, 
			User user) {
		try {
			
			int numCheckins = CheckIn.checkout("isha", memberId, 
					programId, sessionDateYYYYMMDD, 
					Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, numCheckins);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(path = "getNumCheckIns", httpMethod = HttpMethod.GET)
	public APIResponse getNumCheckIns(@Named("programId") long programId, 
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD, 
			@Named("batchNo") int batchNo) {
		try {
			
			int numCheckins = CheckIn.getNumCheckins("isha", programId, sessionDateYYYYMMDD, batchNo);
												
			return new APIResponse(Status.SUCCESS, numCheckins);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
}
